<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <fredbird@fredbird.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
*
* @author J.Ducastel <fredbird@fredbird.org>
* @version 
*/
class wakEvents extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	*/
	function wakEvents() {
		$this->__construct();
	}/**/
	
	/**
	*
	* @access
	*/
	function __construct() {
		parent::__construct();
		$this->deb=&wak::getservice('wakdebug');
		// building events foldernode instances
		//$lib=&wak::getservice('waklib');
		//$lib->needclass('foldernode');
		//$lib->needclass('filenode');
		//$this->corenode=&new foldernode(WAK_CORE_PATH.'events');
		//$this->appnode=&new foldernode('events');
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* executes an event call
	* @param string $eventCall format is event_type.event_id[.call_segment_1][.call_segement_2] etc
	* @param wakParamaters $parameters optional
	* @access public
	* @return mixed event result
	*/
	function & run($eventCall, $parameters=null) {
		$result=false;
		// defines event type
		$levels=explode('.',$eventCall);
		if (!$type=$levels[0]) {
			wakapi::notice("wakevents: can't define event type of $eventCall");
			return $result;
		}
		// getting handler
		if (!$handler=&wak::getservice('wakeventhandler',$type)) {
			wakapi::notice("wakevents: unsupported event type : $type");
			return $result;
		}
		// building paramaters instance if necessary
		if (is_null($parameters)) {
			$parameters=&new wakparameters();
		}
		// running event and returning result
		$result=$handler->run($eventCall,$parameters);
		return $result;
	}/**/
	
	/**
	* search for event file 
	* @param string basename
	* @access public
	* @return wakEventFileNode
	* /
	function & searchEventFileNode($basename) {
		$regex="^$basename.wak-event.php\$"; //echo $regex;
		// searching core, then app
		if (!$results=$this->corenode->regexSearchName($regex)
		and !$results=$this->appnode->regexSearchName($regex)) {
				$eventnode=false;
		} else {
			$eventnode=&$results[0];
		}
		return $eventnode;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return string
	*/
	function searchEventFile($basename) {
		$regex="^$basename.wak-event.php\$"; //echo $regex;
		$fs=&wak::getservice('wakfilesystem');
		// searching core-level
		if ($results=$fs->dirRegexSearchName(WAK_CORE_PATH.'events',$regex,true)) {
			return $results[0];
		}
		// searching app-level
		if ($results=$fs->dirRegexSearchName('app/events',$regex,true)) {
			return $results[0];
		}
		return false;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	var $corenode; // core foldernode instance
	var $appnode; // app foldernode instance
	//var $regexEventFile;
	var $handlers=array(); // event handlers instances
	/*var $cfg=array(
		'handlers'=>array(
			'script'=>''));*/
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/

	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>